package com.ruoyi.auth.controller;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.auth.constants.ValidateSubjectEnum;
import com.ruoyi.auth.verification.service.VerificationCodeService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.utils.sign.Base64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/oauth/vcc")
@Api(tags = "验证码中心接口")
public class VerificationCodeController {

    @Autowired
    private VerificationCodeService verificationCodeService;

    @GetMapping("/image")
    @ApiOperation("生成验证码图片")
    public R validate(HttpServletResponse response) throws IOException {
        String subject = ValidateSubjectEnum.IMAGE.getCode();
        String token = this.verificationCodeService.getToken(subject);
        response.setContentType("image/jpeg");
        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        this.verificationCodeService.renderImage(this.verificationCodeService.getRealToken(token, subject), os);
        Map<String, String> map = new HashMap<>();
        map.put("codeToken", token);
        map.put("img", Base64.encode(os.toByteArray()));
        return R.ok(map);
    }

    @GetMapping("/token")
    @ApiOperation("获取新的验证码")
    public R<String> getToken(String subject) {
        String token = this.verificationCodeService.getToken(subject);
        return R.ok(token);
    }

    @GetMapping("/sendSms")
    @ApiOperation("发送验证码短信")
    public R<String> sendSms(String areaCode, String projectName, String phoneNumber) {
        if (StrUtil.isEmpty(phoneNumber)) {
            return R.fail("手机号码不能为空");
        }
        if (!ReUtil.contains("^[1][0-9][0-9]\\d{8}$", phoneNumber)) {
            return R.fail("手机号码不合法");
        }
        String token = this.verificationCodeService.getToken(phoneNumber);
        this.verificationCodeService.sendSms(areaCode, token, projectName, "login", phoneNumber, "mobileCode", null);
        return R.ok(token);
    }

    @GetMapping("/sendEmail")
    @ApiOperation("发送验证码邮件")
    public R<String> sendEmail(String areaCode, String projectName, String email) {
        if (StrUtil.isEmpty(email)) {
            throw new BaseException("邮箱不能为空");
        }
        if (!ReUtil.contains("^[0-9a-zA-Z]+@[0-9a-zA-Z]+.[0-9a-zA-Z]+$", email)) {
            throw new BaseException("邮箱不合法");
        }
        String token = this.verificationCodeService.getToken(email);
        this.verificationCodeService.sendEmail(areaCode, token, projectName, "login", email, "emailCode", null);
        return R.ok(token);
    }


}
